/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package users.tag;

import java.io.IOException;
import java.util.Collection;
import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyContent;
import javax.servlet.jsp.tagext.BodyTagSupport;
import tracker.TrackerHelper;
import tracker.Users;

/**
 *
 * @author El Desperado
 */
public class RegisterTagHandler extends BodyTagSupport {

    private String userName;
    private String password;

    @Override
    public int doStartTag() throws JspException {
        return super.doStartTag();
    }

    @Override
    public int doEndTag() throws JspException {
        String errorStatus = "<div id=\"status-form\" style=\"margin-left:auto;margin-right:auto;padding-top: 10px;\n"
                + "    text-align: center; color=\"rgba(247, 16, 16, 0.6)\";\"><h3>Status: ";
        System.err.println("Username: " + userName);
        System.err.println("Password: " + password);
        Pattern p = Pattern.compile("^[A-Za-z0-9]+$");
        Matcher m = p.matcher(userName);
        if (m.matches()) {
            try {
                TrackerHelper helper = new TrackerHelper();
                Collection<Users> list = helper.getUserByName(userName);
                Iterator<Users> it = list.iterator();
                while (it.hasNext()) { //Check duplicate username
                    Users usr = it.next();
                    if (usr.getUserName().equals(userName)) {
                        System.out.println("Username is existed! Username: " + userName + " Password: " + password);
                        errorStatus += "Username is existed!";
                        errorStatus += "</h3></div>";
                        try {
                            pageContext.getOut().write(errorStatus);
                        } catch (IOException ex) {
                            Logger.getLogger(RegisterTagHandler.class.getName()).log(Level.SEVERE, null, ex);
                        }
                        return 0;
                    }
                }
                helper.addUser(userName, password);
                errorStatus += "Register Successfully!";
            } catch (Exception e) {
                System.out.println("Error in RegisterTag: " + e.getMessage());
                errorStatus += e.getMessage();
            }
        }
        errorStatus += "</h3></div>";
        try {
            pageContext.getOut().write(errorStatus);
        } catch (IOException ex) {
            Logger.getLogger(RegisterTagHandler.class.getName()).log(Level.SEVERE, null, ex);
        }

        return EVAL_PAGE;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}
